/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: gsingh $
 * $Revision: 1.2 $
 * $Date: 2004/06/01 02:46:15 $
 * (c)Copyright 2004
 * education.au limited 
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its 
 * contributors may be used to endorse or promote products derived from this 
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and 
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising 
 * in any way from the use of the software whether arising from or in 
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is 
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 * 
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.service;

import java.util.Hashtable;

import au.edu.educationau.opensource.dsm.util.Flog;

/**
 * Temporary store for SOAP search results. This exists because the DiskCache
 * cannot store data without a unique URL. FIX ME
 */

public final class SOAPSearchResultCacheService {

	private static SOAPSearchResultCacheService _instance = null;
	private Hashtable results = new Hashtable(200);
	/** Class display string */
	public static final String classStr = "o.m.d.m.SOAPSearchResultCacheService";
	private long reaperInterval = 3600000; // 3600 seconds or 1 hour
	private Reaper reaper = null;

	private SOAPSearchResultCacheService() {
		this.reaperInterval = PropertiesService.getLongProperty("soapsearchresultcacheservice.reaper.interval", 3600000);
		this.reaper = new Reaper();
		Thread t = new Thread(this.reaper);
		t.start();
		Flog.debug(classStr, "SOAPSearchResultCacheService started ok.");
	}

	/**
	 * Returns a singleton instance of the SOAPSearchResultCacheService - use
	 * DeusExMachina to get it
	 */
	public static SOAPSearchResultCacheService getInstance() {
		if (null == _instance) {
			_instance = new SOAPSearchResultCacheService();
		}
		return _instance;
	}

	/** Destroy objects worthy of destruction */
	public void destroy() {
		try {
			results.clear();
		} catch (Exception oe) {
		}
		try {
			this.reaper.stopReaper();
		} catch (Exception oe) {
		}
		Flog.debug(classStr, "SOAPSearchResultCacheService shutdown ok.");
	}

	/**
	 * Returns the results for this adapter and key
	 * 
	 * @param adapter
	 * @param key
	 */
	public Object getResults(String adapter, String key) {
		return results.get(adapter + ":" + key);
	}

	/**
	 * Checks if there are results for this adapter and key pair
	 * 
	 * @param adapter
	 * @param key
	 */
	public boolean hasResults(String adapter, String key) {
		return results.containsKey(adapter + ":" + key);
	}

	/**
	 * Add these results to the entry with this key and adapter
	 * 
	 * @param adapter
	 * @param key
	 * @param result
	 */
	public void addResults(String adapter, String key, Object result) {
		results.put(adapter + ":" + key, result);
	}

	private synchronized void removeEntry(String adapter, String key) {
		results.remove(adapter + ":" + key);
	}

	/** Manages the result cache */
	private class Reaper implements java.lang.Runnable {
		boolean runReaper = true;

		public void stopReaper() {
			runReaper = false;
		}

		public void run() {
			while (runReaper) {
				try {
					results.clear();
				} catch (Exception oe) {
					Flog.error(classStr + ".Reaper", "Error while maintaining results", oe);
				}

				try {
					Thread.currentThread().sleep(reaperInterval);
				} catch (InterruptedException ie) {
				}
			}
		}
	}
}

// -- EOF

